package com.tianqu.tms.www.bid.controller;

import java.text.ParseException;
import java.util.Date;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.AucManagement;

public class BidCarQuotationInfoAjaxController extends TmsController {

	private static final String RESPONSETEXT = "responseText";
	private Logger logger = Logger.getLogger(BidCarQuotationInfoAjaxController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			
			controller.begin();
			
			// 竞价管理
			AucManagement aucManage = AucManagement.getInstance();
			
			// 车辆ID
			int id = StringUtil.getint(uc.getParameter("carId"));
			
			StringBuffer sb = new StringBuffer();
				
			// 根据车辆ID取得车辆信息
			AucManagement auc = aucManage.getAucInfoById(id);

			// 最高出价(万元)
			int finalExpetPrices = auc.getFinalExpetPrices();
			if (finalExpetPrices == 0) {
				sb.append("0");
			} else {
				sb.append(StringUtil.transYtoW(finalExpetPrices));
			}
			sb.append(",");
			
			//出价商户数量
			sb.append(auc.getExpetPricesNum());
			sb.append(",");
	
			// 竞价开始时间
			Date startTime = new Date(auc.getStartTime().getTime());
			
			// 竞价时长
			int auctionDeadline = auc.getAuctionDeadline();
			
			// 剩余时间（秒）
			String[] djs = getdjs(startTime,auctionDeadline);
			sb.append(djs[0]);
			sb.append(",");
			sb.append(djs[1]);
			sb.append(",");

			// 竞价状态
			if ("0".equals(djs[0])) {
				sb.append("0");
			} else {
				sb.append("1");
			}

			controller.commit();
			String responseText = sb.toString();
			uc.setAttribute(RESPONSETEXT, responseText);

		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 取得剩余时间（秒）
	 * 
	 * @param start 竞价开始时间
	 * @param auctionDeadline 竞价时长
	 * @return String[]
	 */
	private String[] getdjs(Date start,int auctionDeadline) throws ParseException {
			
		// 当前时间
		Date nowDate = new Date();
		
		// 开始时间  + 竞价时长 + 1分钟
		Date deadDate= DateUtil.string101ToDate(DateUtil.getTimeByMinute(start,auctionDeadline + 1));
		
		// 剩余时间
		long djs = (deadDate.getTime() - nowDate.getTime())/1000;
		
		String[] djsStr = {"0","秒"};
		
		if (djs < 0) {
			
			// 剩余时间60秒以内			
			djsStr[0] = "0";
			djsStr[1] = "秒";
			
		} else if (djs < 60) {
			
			// 剩余时间60秒以内			
			djsStr[0] = String.valueOf(djs);
			djsStr[1] = "秒";
			
		} else {
			
			// 剩余时间1分钟以上
			if (djs % 60 == 0) {
				djsStr[0] = String.valueOf(djs / 60);
				
			} else {
				djsStr[0] = String.valueOf(djs / 60 + 1);
			}
			djsStr[1] = "分";
		}
		
		return djsStr;
	}
}
